package com.powernode.model;

import com.powernode.constant.BusinessEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Description: 项目统一响应结果
 * @Author: Tong Ziyu
 * @Date: 2024/10/8 22:20
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("项目统一响应结果对象")
public class Result<T> implements Serializable {

    @ApiModelProperty("状态码")
    private Integer code = 200;

    @ApiModelProperty("响应信息")
    private String msg = "ok";

    @ApiModelProperty("响应数据")
    private T data;

    public static <T> Result<T> success(T data){
        Result result = new Result<>();
        result.setData(data);
        return result;
    }

    public static <T> Result<T> fail(Integer code,String msg){
        Result result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(null);
        return result;
    }

    public static <T> Result<T> fail(BusinessEnum businessEnum){
        Result result = new Result<>();
        result.setCode(businessEnum.getCode());
        result.setMsg(businessEnum.getDesc());
        result.setData(null);
        return result;
    }


    public static Result<String> handle(boolean flag){
        if (flag){
            return Result.success(null);
        }else{
            return Result.fail(BusinessEnum.OPERATION_FAIL);
        }
    }
}
